﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Telerik.Web.UI;
using HandBook.Data;
using HandBook.Common;
using System.Text.RegularExpressions;

public partial class Admin_FoodBookDetails_Admin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["FoodBookID"] == null)
            {
                DetailsView1.DefaultMode = DetailsViewMode.Insert;
                Button3.Text = "Insert";
            }
            else
            {
                DetailsView1.DefaultMode = DetailsViewMode.Edit;
                DataView dvSql = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                foreach (DataRowView drvSql in dvSql)
                {
                    string LongDes = drvSql["LongDescription"].ToString();
                    string Pattern = "-->";
                    Regex myRegex = new Regex(Pattern);
                    string[] sKetQua = myRegex.Split(LongDes);
                    string nl = sKetQua[1].ToString();
                    string th = sKetQua[2].ToString();
                    (DetailsView1.FindControl("RadEditorNguyenLieuEdit") as RadEditor).Content = nl.Trim();
                    (DetailsView1.FindControl("RadEditorThucHienEdit") as RadEditor).Content = th.Trim();
                }
                Button3.Text = "Update";
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (Button3.Text == "Update")
        {
            UpdateFoodBook();            
        }
        else if(Button3.Text == "Insert") 
        {
            InsertFoodBook();
        }
        ScriptManager.RegisterStartupScript(this, GetType(), "closeWnd", "reloadParentPage();", true);
    }
    private void UpdateFoodBook() 
    {
        int foodBookID = Convert.ToInt32(Request.Params["FoodBookID"].ToString());
        var foodBook = HandBookData.EatDrinkDB.FoodBooks.Single(f => f.FoodBookID == foodBookID);
        foodBook.FoodStyleID = Convert.ToInt32((DetailsView1.FindControl("ddlFoodStyleID") as DropDownList).SelectedValue);
        foodBook.Title = (DetailsView1.FindControl("txtTitle") as TextBox).Text.Trim();
        foodBook.ShortDescription = (DetailsView1.FindControl("RadEditorShort") as RadEditor).Content.Trim();
        string nguyenLieu = (DetailsView1.FindControl("RadEditorNguyenLieuEdit") as RadEditor).Content.Trim();
        string thucHien = (DetailsView1.FindControl("RadEditorThucHienEdit") as RadEditor).Content.Trim();
        foodBook.LongDescription = "-->" + nguyenLieu.Trim() + "-->" + thucHien.Trim();
        string fileUpload = (DetailsView1.FindControl("FileUpload1") as FileUpload).FileName.Trim();
        if (!fileUpload.Equals("")) 
        {
            string file = HandBook.Common.FileManager.GetFileName(fileUpload);
            string path = Server.MapPath("~/Images/Foods/" + file);
            (DetailsView1.FindControl("FileUpload1") as FileUpload).SaveAs(path);
            foodBook.Image = file;
        }
        foodBook.IsShowHomePage = Convert.ToBoolean((DetailsView1.FindControl("ddlIsActivated") as DropDownList).SelectedValue.ToString());
        HandBookData.EatDrinkDB.SubmitChanges();
    }
    private void InsertFoodBook() 
    {
        FoodBook food = new FoodBook();
        food.FoodStyleID = Convert.ToInt32((DetailsView1.FindControl("ddlFoodStyleID") as DropDownList).SelectedValue);        
        food.Date = Convert.ToDateTime(DateTime.Now.ToString());
        food.Title = (DetailsView1.FindControl("txtTitle") as TextBox).Text.Trim();
        food.ShortDescription = (DetailsView1.FindControl("RadEditorShort") as RadEditor).Content.Trim();
        string nguyenLieu = "<b>" + "Nguyên Liệu :" + "</b>" + "</br>" + (DetailsView1.FindControl("RadEditorNguyenLieuInsert") as RadEditor).Content.Trim();
        string thucHien = "<b>" + "Thực Hiện :" + "</b>" + "</br>" + (DetailsView1.FindControl("RadEditorThucHienInsert") as RadEditor).Content.Trim();
        food.LongDescription = "-->" + nguyenLieu + "-->" + thucHien;
        string fileUpload = (DetailsView1.FindControl("FileUpload1") as FileUpload).FileName.Trim();
        if (!fileUpload.Equals(""))
        {
            string file = HandBook.Common.FileManager.GetFileName(fileUpload);
            string path = Server.MapPath("~/Images/Foods/" + file);
            (DetailsView1.FindControl("FileUpload1") as FileUpload).SaveAs(path);
            food.Image = file;
        }
        food.IsShowHomePage = Convert.ToBoolean((DetailsView1.FindControl("ddlIsActivated") as DropDownList).SelectedValue.ToString());
        HandBookData.EatDrinkDB.FoodBooks.InsertOnSubmit(food);
        HandBookData.EatDrinkDB.SubmitChanges();
    }
}
